/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Forms;

import BLL.BLLBenhNhan;
import BLL.BLLPhongKham;
import DTO.DTOBenhNhan;
import DTO.DTOPhongKham;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import javax.swing.DefaultComboBoxModel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Documents
 */
public class ThongKe extends javax.swing.JFrame {

    BLLPhongKham bllPhong = new BLLPhongKham();
    BLLBenhNhan bllBenhNhan = new BLLBenhNhan();
    DefaultTableModel modelBenhNhan;
    /**
     * Creates new form ThongKe
     */
    public ThongKe() {
        initComponents();
        
         //khởi tạo các phòng khám vào combobox
        DefaultComboBoxModel modelPhong = new DefaultComboBoxModel();
        DTOPhongKham allPhong = new DTOPhongKham();
        allPhong.setMapk(0);
        allPhong.setTenpk("Tất cả các phòng");
        modelPhong.addElement(allPhong);
        for(DTOPhongKham dtoPhong : bllPhong.layDSPhongKham()){
            modelPhong.addElement(dtoPhong);
        }
        jcbPhong.setModel(modelPhong);
        
        modelBenhNhan = new DefaultTableModel();
        String[] cols = {"Mã BN", "Họ tên", "Giới tính", "Năm sinh", "CMND", "BHYT"};//tiêu đề cột
        modelBenhNhan.setColumnIdentifiers(cols);
        
        //set model cho table bệnh nhân
        jtableBN.setModel(modelBenhNhan);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jcbNgay = new javax.swing.JComboBox();
        jcbThang = new javax.swing.JComboBox();
        jbtnThongKe = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jcbPhong = new javax.swing.JComboBox();
        jcbNam = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtableBN = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        lbSoLuot = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        lbBHYT = new javax.swing.JLabel();
        lbVuotTuyen = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 102, 0));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("THỐNG KÊ");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jLabel2.setText("Chọn ngày");

        jcbNgay.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));
        jcbNgay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbNgayActionPerformed(evt);
            }
        });

        jcbThang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" }));

        jbtnThongKe.setText("Thống kê");
        jbtnThongKe.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnThongKeActionPerformed(evt);
            }
        });

        jLabel10.setText("Chọn phòng khám");

        jcbPhong.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Chọn phòng khám", "Bệnh cơ xương khớp", "Bệnh da liễu", "Bệnh mắt", "Bệnh ngoại tổng quát", "Bệnh nhiệt đới", "Bệnh nội thần kinh", "Bệnh nội tiết", "Bệnh răng hàm mặt", "Bệnh tai mũi họng", "Bệnh tiêu hoá", "Bệnh tim mạch", "Khám và chứng nhận sức khoẻ", "Ngoại thần kinh, thận niệu", "Sản phụ khoa và kế hoạch hóa gia đình" }));

        jcbNam.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014" }));
        jcbNam.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbNamActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jcbNgay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jcbThang, 0, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jcbNam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(107, 107, 107)
                .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jcbPhong, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(40, 40, 40)
                .addComponent(jbtnThongKe)
                .addContainerGap(165, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jcbNgay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jcbThang, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnThongKe)
                    .addComponent(jLabel10)
                    .addComponent(jcbNam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jcbPhong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Kết quả"));

        jtableBN.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jtableBN.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Ngày khám", "Lượt khám", "Có BHYT", "Vượt tuyến"
            }
        ));
        jScrollPane2.setViewportView(jtableBN);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE))
        );

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("Tổng số lượt khám:");

        lbSoLuot.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel9.setText("lượt");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel4.setText("Tổng số có BHYT:");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel5.setText("Tổng số khám vượt tuyến:");

        lbBHYT.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N

        lbVuotTuyen.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(28, 28, 28)
                                .addComponent(lbSoLuot, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel4)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(lbBHYT, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel5)
                                    .addGap(18, 18, 18)
                                    .addComponent(lbVuotTuyen, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(381, 381, 381)
                .addComponent(jLabel1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lbSoLuot, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addComponent(jLabel4))
                    .addComponent(lbBHYT, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lbVuotTuyen, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(49, 49, 49))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jcbNgayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbNgayActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jcbNgayActionPerformed

    private void jcbNamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbNamActionPerformed
        java.util.Date date = new java.util.Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy");
        int curretYear = Integer.valueOf(format.format(date));
        int namsinh = Integer.valueOf(jcbNam.getSelectedItem().toString());
        int tuoi = curretYear - namsinh;
        
    }//GEN-LAST:event_jcbNamActionPerformed

    private void jbtnThongKeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnThongKeActionPerformed
        try {
            //xóa danh sách cũ
            for(int i = modelBenhNhan.getRowCount() - 1; i >= 0; i--){
                modelBenhNhan.removeRow(i);
            }
            
            DTOBenhNhan dtoBN = new DTOBenhNhan();
            Date ngay = layNgayThongKe();
            DTOPhongKham dtoPhong = (DTOPhongKham)jcbPhong.getSelectedItem();
            dtoBN.setNgaydangky(ngay);
            dtoBN.setDtoPhongKham(dtoPhong);
            ArrayList<DTOBenhNhan> dsBenhNhan = bllBenhNhan.thongkeBenhNhan(dtoBN);
            //cập nhật danh sách mới
            for(DTOBenhNhan dtoBenhNhan : dsBenhNhan){
                modelBenhNhan.addRow(new Object[]{dtoBenhNhan.getMaBN(), dtoBenhNhan.getHoten(), dtoBenhNhan.getGioitinh(),
                                       dtoBenhNhan.getNgaysinh(), dtoBenhNhan.getCMND(), dtoBenhNhan.getBHYT()==1?"Có":"Không" });
             }
            
            //đếm số lượt khám
            int dem = modelBenhNhan.getRowCount();
            
            
            //số lượt có BHYT && vuot tuyen
            int bhyt = 0;
            int vuottuyen = 0;
            for(DTOBenhNhan dtoBenhNhan : dsBenhNhan){
                if(dtoBenhNhan.getBHYT() == 1)
                    bhyt++;
                if(dtoBenhNhan.getVuottuyen() == 1)
                    vuottuyen++;
            }
            
            //hien thi
            lbSoLuot.setText(String.valueOf(dem));
            lbBHYT.setText(String.valueOf(bhyt));
            lbVuotTuyen.setText(String.valueOf(vuottuyen));
            
        } catch (Exception e) {
        }
    }//GEN-LAST:event_jbtnThongKeActionPerformed

    //lay ngay thống ke
    private Date layNgayThongKe(){
        try {
            int namHH = Integer.valueOf(jcbNam.getSelectedItem().toString());
            int thangHH = Integer.valueOf(jcbThang.getSelectedItem().toString());
            int ngayHH = Integer.valueOf(jcbNgay.getSelectedItem().toString());
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");//định dạng kiểu ngày tháng năm
            format.setLenient(false);//không cho phép lấy ngày qua tháng sau, bắt buộc phải chọn đúng ngày
            java.util.Date date = format.parse(namHH + "-" + thangHH + "-" + ngayHH);
            Date setNgayHH = new Date(date.getTime());//đổi ngày từ java.util.Date sang java.sql.Date
            return setNgayHH;
        } catch (NumberFormatException | ParseException e) {
            return null;
        }
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ThongKe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ThongKe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ThongKe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ThongKe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ThongKe().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton jbtnThongKe;
    private javax.swing.JComboBox jcbNam;
    private javax.swing.JComboBox jcbNgay;
    private javax.swing.JComboBox jcbPhong;
    private javax.swing.JComboBox jcbThang;
    private javax.swing.JTable jtableBN;
    private javax.swing.JLabel lbBHYT;
    private javax.swing.JLabel lbSoLuot;
    private javax.swing.JLabel lbVuotTuyen;
    // End of variables declaration//GEN-END:variables
}
